import { getSideBarState, setSideBarState } from '@/utils/storage'
import router from '@/router'

export default {
  namespaced: true,

  state: {
    sideBarState: getSideBarState() || 'close',

    chatTitle: '用户询问助手功能', // 顶部标题

    isRenderChatList: false, // 是否渲染对话列表，根据对话id获取到数据时，为渲染列表，不展示打印效果，普通对话时展示打印效果
    chatList: [] // 对话列表
  },

  mutations: {
    SET_sideBarState(state, val) {
      state.sideBarState = val
      setSideBarState(val)
    },

    SET_chatTitle(state, val) {
      state.chatTitle = val || '用户询问助手功能'
    },

    SET_isRenderChatList(state, val) {
      state.isRenderChatList = val
    },

    SET_chatList(state, val) {
      /* 请求数据状态为加载中，添加数据
      加载完成，替换当前项 */
      if (Array.isArray(val)) {
        state.chatList = val
      } else if (val.loadingChat) {
        state.chatList.push(val)
      } else {
        state.chatList.splice(-1, 1, val)
      }
    },

    OPEN_newChat(state) {
      state.chatList = []
      router.push({ name: 'index' })
    }
  }
}
